package faf.web.tests;

import static org.junit.Assert.*;

import java.util.HashMap;

import javax.persistence.EntityManager;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import faf.business.SheetManager;
import faf.business.UserManager;
import faf.dao.JpaUtils;
import faf.entities.Account;
import faf.entities.Sheet;
import faf.entities.Survey;
import faf.web.LandingController;

public class LandingControllerTests {
	static JpaUtils jpautils;
	static UserManager userManager;
	static SheetManager sheetManager;
	static LandingController landingController;
	static Account simpleUser;
	static Sheet sheet;

	@BeforeClass
	public static void beforeClass() throws Exception {
		HashMap<String, String> config = new HashMap<String, String>();
		config.put("hibernate.hbm2ddl.auto", "create-drop");

		// business layer injections

		jpautils = new JpaUtils(config);
		userManager = new UserManager();
		userManager.setJpautils(jpautils);
		sheetManager = new SheetManager();
		sheetManager.setJpautils(jpautils);
		landingController = new LandingController();
		landingController.setSheetManager(sheetManager);
		landingController.setUserManager(userManager);

		// simple user creation

		simpleUser = new Account();
		simpleUser.setFirstname("Test");
		simpleUser.setLastname("Test");
		simpleUser.setMail("test@test.fr");
		simpleUser.setPassword("test");
		simpleUser = userManager.createAccount(simpleUser);

		// empty survey creation and affectation

		Survey survey = new Survey();
		sheet = new Sheet();

		sheet.setAccount(simpleUser);
		sheet.setSurvey(survey);

		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(survey);
		em.persist(sheet);
		em.getTransaction().commit();
	}

	@Test
	public void testHandleRequestWithGoodToken() throws Exception {
		MockHttpServletRequest request = new MockHttpServletRequest("GET",
				"landing.htm");
		request.addParameter("token", simpleUser.getToken());
		MockHttpServletResponse response = new MockHttpServletResponse();
		ModelAndView modelAndView = landingController.handleRequest(request,
				response);
		assertEquals(modelAndView.getViewName(), "home");
		Account accountLogged = (Account) request.getSession().getAttribute(
				"user");
		assertEquals(accountLogged.getToken(), simpleUser.getToken());
	}

	@Test
	public void testHandleRequestWithBadToken() throws Exception {
		MockHttpServletRequest request = new MockHttpServletRequest("GET",
				"landing.htm");
		request.addParameter("token", "bad-token");
		MockHttpServletResponse response = new MockHttpServletResponse();
		ModelAndView modelAndView = landingController.handleRequest(request,
				response);
		assertEquals(modelAndView.getViewName(), "landing");
		assertNull(request.getSession().getAttribute("user"));
	}

	@Test
	public void testRedirectionToSheetWithGoodToken() throws Exception {
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "landing.htm");
		request.addParameter("token", simpleUser.getToken());
		request.addParameter("sheet", String.valueOf(sheet.getId()));
		assertNotNull(request);
		MockHttpServletResponse response = new MockHttpServletResponse();
		assertNotNull(response);
		ModelAndView modelAndView = landingController.handleRequest(request, response);

		RedirectView redirectView = (RedirectView) modelAndView.getView();
		assertEquals(redirectView.getUrl(), "sheet.htm?id=" + sheet.getId());
		
		assertNotNull(request.getAttribute("questions"));

		Account accountLogged = (Account) request.getSession().getAttribute("user");
		assertEquals(accountLogged.getToken(), simpleUser.getToken());
	}

	@Test
	public void testRedirectionToSheetWithBadToken() throws Exception {
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "landing.htm");
		request.addParameter("token", "bad-token");
		request.addParameter("sheet", String.valueOf(sheet.getId()));
		assertNotNull(request);
		MockHttpServletResponse response = new MockHttpServletResponse();
		assertNotNull(response);
		ModelAndView modelAndView = landingController.handleRequest(request, response);
		assertEquals(modelAndView.getViewName(), "landing");
		assertNull(request.getSession().getAttribute("user"));
	}
	
	
}
